#pragma once
#include<iostream>
using namespace std;

namespace buluo
{
	template<class T>
	struct list_node
	{
		list_node<T>* _prev;
		list_node<T>* _next;
		T _data;
		list_node(const T& x= T())
			:_prev(nullptr)
			,_next(nullptr)
			, _data(x)
		{}
	};

	template<class T, class Ref>
	struct __list_iterator
	{
		typedef list_node<T> node;
		typedef __list_iterator<T, Ref> iterator;
		node* _node;

		__list_iterator(node* x)
			:_node(x)
		{}

		Ref operator*() 
		{
			return _node->_data;
		}
		iterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		iterator& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		iterator operator++(int)
		{
			iterator tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		iterator operator--(int)
		{
			iterator tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const iterator& x)
		{
			return _node != x._node;
		}
		bool operator==(const iterator& x)
		{
			return _node == x._node;
		}
	};
	template<class T>
	class List
	{
	public:
		typedef list_node<T> node;
		typedef __list_iterator<T, T&> iterator;
		typedef __list_iterator<T, const T&> const_iterator;
 		List()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		void push_back(const T& x = T())
		{
			node* newnode = new node(x);
			node* tail = _head->_prev;
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
		}

	private:
		node* _head;
	};
	void print_list(const List<int>& lt)
	{
		List<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			//(*it) *= 2;
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_list()
	{
		List<int> It;
		It.push_back(1);
		It.push_back(2);
		It.push_back(3);
		It.push_back(4);
		print_list(It);
		List<int>::iterator it = It.begin();
		while (it != It.end())
		{
			(*it) *= 2;
			cout << *it << " ";
			++it;
		}

	}
}